package com.neuedu.yyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuedu.yyzx.config.JWTUtil;
import com.neuedu.yyzx.entity.User;
import com.neuedu.yyzx.mapper.UserMapper;
import com.neuedu.yyzx.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author 第7组....
 * @since 2021-05-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Resource
    IUserService userService;
    @Override
    public String token(User user) throws JsonProcessingException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //根据用户名查询 用户对象

        wrapper.eq("username", user.getUsername());
        User queryUser = this.getOne(wrapper); //根据用户名 查询 用户 ， 携带者 密码（秘闻） $2a$10$0nYMHpHlofHtVCmZcWhPGuX5aRTg8XJnht1VT/iHOdRuOQZMqcMoC
        if (queryUser != null) {

            //如果用户名存在则验证密码
            //如果密码一致则认为登录成功
            if (bCryptPasswordEncoder.matches(user.getPassword(), queryUser.getPassword())) {
                //将用户信息生成token数据返回给前端

                LocalDateTime now = LocalDateTime.now();

                String token = JWTUtil.create(queryUser);

                //保存登录状态信息 --》 ssdb
                //user-admin

//                updateLastLogin(queryUser.getId(), now);

                //缓存用户登录信息
                write2SSDB(queryUser, now);

                return token;
            }
        }

        return null;
    }

    @Override
    public Object logout(HttpServletRequest request) {
        return null;
    }

    private void write2SSDB(User user, LocalDateTime now) throws JsonProcessingException {
        Map info = new HashMap<>();
        info.put("username", user.getUsername());
        info.put("password", user.getPassword());
        info.put("id", user.getId());



        ObjectMapper objectMapper = new ObjectMapper();
        String key = JWTUtil.KEY + "-" + user.getUsername();
        String userJson = objectMapper.writeValueAsString(info);


    }


//    private void updateLastLogin(Integer id, LocalDateTime now) {
//        User user = new User();
//        user.setId(id);
//        user.set
//        updateById(user);
//    }

    @Override
    public List<User> list(User user) {
        // 如果 有name传过来 就按照name 模糊查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(user.getUsername())) {
            wrapper.like("username",user.getUsername());
        }


        // 如果分页返回 IPage 如果不分页 返回 List

            return this.list(wrapper);


    }

    @Override
    public boolean add(User user) {
        return false;
    }

    @Override
    public Object batchdel(Integer[] ids) {
        return null;
    }


}